package com.box.service.impl;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.box.entity.AdviceSearch;
import com.box.entity.DbAdvice;
import com.box.entity.DbAdviceExample;
import com.box.entity.DbAdviceExample.Criteria;
import com.box.mapper.DbAdviceMapper;
import com.box.service.AdviceService;
import com.box.utils.LayuiResult;
import com.box.utils.MyUtil;
import com.box.utils.NoticeGlobal;
import com.box.utils.NoticePage;
import com.box.utils.NoticePageView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.reflect.TypeToken;

/**
*<p>Title:AdviceServiceImpl.java</p>
*<p>Description:</p>
*<p>Company:www.99weixinxcx.cn</p>
*@author 陕西电子科技学院:滕一帆
*@date	2019年5月9日上午3:25:17
*@version 1.0
*
*
*
 */
@Service
public class AdviceServiceImpl implements AdviceService {
	
	@Autowired
	private DbAdviceMapper adviceMapper;

	public LayuiResult selAdviceBySearch(Integer page, Integer limit,
			AdviceSearch search) {
		// TODO Auto-generated method stub
		try {
			PageHelper.startPage(page, limit);
			DbAdviceExample examlpe=new DbAdviceExample();
			examlpe.setOrderByClause("updatetime desc");
			Criteria createCriteria = examlpe.createCriteria();
			//公告标题不为空
			if(search.getAdvtitle()!=null&&!search.getAdvtitle().equals("")){
				createCriteria.andAdvtitleLike("%"+search.getAdvtitle()+"%");
				
			}
			if(search.getAid()!=null&&!"-1".equals(search.getAid())){
				createCriteria.andAidEqualTo(Integer.parseInt(search.getAid()));
			}		
			if(search.getAdvdescribe()!=null&&!search.getAdvdescribe().equals("")){
				createCriteria.andAdvdescribeLike("%"+search.getAdvdescribe()+"%");
			}
			if(search.getCreateTimeStart()!=null&&!"".equals(search.getCreateTimeStart())){
				
				createCriteria.andUpdatetimeGreaterThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeStart()));
			}
			if(search.getCreateTimeEnd()!=null&&!"".equals(search.getCreateTimeEnd())){
				createCriteria.andUpdatetimeLessThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeEnd()));
			}
			List<DbAdvice> list= adviceMapper.selectByExampleWithBLOBs(examlpe);
			PageInfo<DbAdvice> pageinfo=new PageInfo<DbAdvice>(list);
			LayuiResult result=new LayuiResult();
			result.setCode(0);
			result.setCount(pageinfo.getTotal());
			result.setMsg("成功获取数据列表");
			result.setData(pageinfo.getList());
			return result;
		} catch (Exception e) {
			// TODO: handle exception
			LayuiResult result=new LayuiResult();
			result.setCode(0);
			result.setCount(null);
			result.setMsg("失败");
			result.setData(e);
			return result;
		}
		//

	}

	public LayuiResult delOneByAdvice(String advid) {
		// TODO Auto-generated method stub
		try {
			Integer n=adviceMapper.deleteByPrimaryKey(Integer.parseInt(advid));
			System.out.println("成功删除:"+n);
			
			return new LayuiResult(0, "成功删除一条记录");
		} catch (Exception e) {
			// TODO: handle exception
			return new LayuiResult(500, ""+e);
		}
		
	}

	public LayuiResult delDataByAdvieForOne(String adviceStr) {
		// TODO Auto-generated method stub
		try {
			String[] ids = adviceStr.split(",");
			Integer n=0;
			if(ids!=null&&ids.length>0){
				for (String advid : ids) {
					
				n=	adviceMapper.deleteByPrimaryKey(Integer.parseInt(advid));
				}
				System.out.println("成功批量删除数据"+n);
			}
			return new LayuiResult(0, "成功批量删除数据");
		} catch (Exception e) {
			// TODO: handle exception
			return new LayuiResult(500, "批量删除数据失败");
		}
		
		
	}

	public DbAdvice selAdviceDataById(String advid) {
		// TODO Auto-generated method stub
		DbAdviceExample example=new DbAdviceExample();
		example.setOrderByClause("updatetime desc");
		Criteria createCriteria = example.createCriteria();
		Integer id=Integer.parseInt(advid);

			createCriteria.andAdvidEqualTo(id);
		return 	adviceMapper.selectByExampleWithBLOBs(example).get(0);
	
	
		
	
	}

	public LayuiResult AddAdvice(DbAdvice advice) {
		// TODO Auto-generated method stub
		try {
			advice.setUpdatetime(new Date());
		Integer n=	adviceMapper.insert(advice);
		System.out.println("成功添加记录:"+n);
			return new LayuiResult(0, "成功添加记录");
		} catch (Exception e) {
			// TODO: handle exception
			return new LayuiResult(500, "记录添加失败");
		}
	
	}

	public LayuiResult UpdateAdvice(DbAdvice advice) {
		// TODO Auto-generated method stub
		try {
			advice.setUpdatetime(new Date());
	Integer n=		adviceMapper.updateByPrimaryKeySelective(advice);
	System.out.println("成功修改:"+n);
			return new LayuiResult(0, "成功修改记录");
		} catch (Exception e) {
			// TODO: handle exception
			return new LayuiResult(500, "修改记录失败");
		}
		
	}

	public List<DbAdvice> selAdviceList() {
		// TODO Auto-generated method stub
		DbAdviceExample example =new DbAdviceExample();
		example.setOrderByClause("updatetime desc");
		List<DbAdvice> list = adviceMapper.selectByExampleWithBLOBs(example);
		return list;
	}

	public NoticePage selAdviceByAjax(Integer page) {
		// TODO Auto-generated method stub
	try {
		PageHelper.startPage(page, NoticeGlobal.SHOWPAGES);
		DbAdviceExample example=new DbAdviceExample();
		example.setOrderByClause("updatetime desc");
		Criteria createCriteria = example.createCriteria();
		List<DbAdvice> list = adviceMapper.selectByExampleWithBLOBs(example);
		PageInfo<DbAdvice> pageinfo=new PageInfo<DbAdvice>(list);
		long total = pageinfo.getTotal();
		
		NoticePageView<DbAdvice> pageview=new NoticePageView<DbAdvice>(page, (int)total, list);
		Type listType = new TypeToken<NoticePageView<DbAdvice>>(){}.getType();
		NoticePage result=new NoticePage();
		result.setPageView(pageview);;
		result.setListType(listType);
		
		
		return result;
	} catch (Exception e) {
		NoticePage result=new NoticePage();
		result.setPageView(null);;
		result.setListType(null);
		return result;
	}

	}

}
